package com.mdb.open.epay.resource;

import cn.hutool.core.date.DatePattern;
import com.mdb.bc.api.data.ListResultEx;
import com.mdb.bc.api.data.ResultEx;
import com.mdb.bc.logger.LogAnnotation;
import com.mdb.bc.utils.DateTimeUtil;
import com.mdb.bc.utils.ObjectUtil;
import com.mdb.bc.web.RepeatSubmitTypeEnum;
import com.mdb.bc.web.annotation.DenyRepeatSubmit;
import com.mdb.bc.web.support.SuperResources;
import com.mdb.open.epay.domain.bo.PayBo;
import com.mdb.open.epay.domain.bo.QueryOrderBo;
import com.mdb.open.epay.domain.bo.RefundBo;
import com.mdb.open.epay.domain.dto.epay.PayDto;
import com.mdb.open.epay.domain.dto.epay.QueryOrderDto;
import com.mdb.open.epay.domain.dto.epay.RefundDto;
import com.mdb.open.epay.domain.vo.epay.OrderVo;
import com.mdb.open.epay.domain.vo.epay.PayVo;
import com.mdb.open.epay.domain.vo.epay.RefundVo;
import com.mdb.open.epay.facade.epay.TradeFacade;
import com.mdb.user.LoginUserInfoHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 *  交易
 *
 * @author liuyi
 * @date 2019-11-14
 */
@RestController
@RequestMapping("")
@Api(tags = "交易")
public class TradeResource extends SuperResources {

    @Autowired
    private TradeFacade tradeFacade;

    @Autowired
    private LoginUserInfoHelper loginUserInfoHelper;

    @DenyRepeatSubmit(type = RepeatSubmitTypeEnum.EXCLUSIVE_SUBMIT)
    @LogAnnotation("消费支付")
    @ApiOperation("消费支付")
    @PostMapping("/qp/pay")
    public ResultEx<PayVo> qpPay(@RequestBody PayDto dto) {
        String customerCode = loginUserInfoHelper.getCustomerCode(true);
        PayBo payBo = new PayBo();
        payBo.setCustomerCode(customerCode);
        payBo.setCustType(dto.getCustType());
        payBo.setOrderNo(dto.getOrderNo());
        payBo.setOrderAmt(dto.getOrderAmt());
        payBo.setOrderTime(dto.getOrderTime());
        payBo.setCurrency(dto.getCurrency());
        payBo.setBankSignNo(dto.getBankSignNo());
        payBo.setTradeType(dto.getTradeType());
        payBo.setMsgCode(dto.getMsgCode());
        payBo.setRemark(dto.getRemark());
        return tradeFacade.qpPay(payBo);
    }

    @DenyRepeatSubmit(type = RepeatSubmitTypeEnum.EXCLUSIVE_SUBMIT)
    @LogAnnotation("退款/撤销")
    @ApiOperation("退款/撤销")
    @PostMapping("/qp/refund")
    public ResultEx<RefundVo> qpRefund(@RequestBody RefundDto dto) {
        //判斷退款/撤銷
        String customerCode = loginUserInfoHelper.getCustomerCode(true);
        RefundBo refundBo = new RefundBo();
        refundBo.setCustomerCode(customerCode);
        refundBo.setCustType(dto.getCustType());
        refundBo.setBizType(dto.getBizType());
        refundBo.setOriOrderNo(dto.getOriOrderNo());
        refundBo.setCurrency(dto.getCurrency());
        refundBo.setReOrderNo(dto.getReOrderNo());
        refundBo.setBackReAmt(dto.getBackReAmt());
        refundBo.setRemark(dto.getRemark());
        return tradeFacade.qpRefund(refundBo);
    }

    @DenyRepeatSubmit(type = RepeatSubmitTypeEnum.EXCLUSIVE_SUBMIT)
    @LogAnnotation("订单查询")
    @ApiOperation("订单查询")
    @GetMapping("/qp/query")
    public ListResultEx<OrderVo> qpQuery(@ModelAttribute QueryOrderDto dto) {
        String customerCode = loginUserInfoHelper.getCustomerCode(true);
        QueryOrderBo queryOrderBo = new QueryOrderBo();
        queryOrderBo.setCustomerCode(customerCode);
        queryOrderBo.setSearchType(dto.getSearchType().getValue());
        queryOrderBo.setBizType(dto.getBizType().getValue());
        queryOrderBo.setBeginDate(ObjectUtil.isEmpty(dto.getBeginDate()) ? null : DateTimeUtil.convert2LocalDateTime(dto.getBeginDate(), DatePattern.PURE_DATETIME_PATTERN));
        queryOrderBo.setEndDate(ObjectUtil.isEmpty(dto.getEndDate()) ? null : DateTimeUtil.convert2LocalDateTime(dto.getEndDate(), DatePattern.PURE_DATETIME_PATTERN));
        queryOrderBo.setOrderNoList(dto.getOrderNoList());
        return tradeFacade.qpQuery(queryOrderBo);
    }

}
